﻿/*----------------------------------------------------------------

//----------------------------------------------------------------*/
using System;
using System.Data.Common;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;

using FamilyTree.DAL;
using FamilyTree.Entity;
using FamilyTree.DLL;

namespace FamilyTree.BLL
{
    /// <summary>
    /// 提供处理各种管理家族信息 FamilyInfo  [TB_FamilyInfo]表的业务逻辑的方法
    /// </summary>
	/// <author>jinchuanliang 2018-07-14</author>
    public sealed class BFamilyInfo : WebDataAccess
    {
        /// <summary>
        /// 私有的构造函数。
        /// </summary>
        private BFamilyInfo()
        {
        }

        /// <summary>
        /// 公共的入口。
        /// </summary>
        /// <returns></returns>
        public static BFamilyInfo BLL
        {
            get { return Bll; }
        }

        /// <summary>
        /// 业务逻辑层对象
        /// </summary>
        private readonly static BFamilyInfo Bll = new BFamilyInfo();

        /// <summary>
        /// 数据访问层对象
        /// </summary>
        internal readonly static DFamilyInfo Dal = new DFamilyInfo();


        /// <summary>
        /// 添加家族信息 FamilyInfo数据
        /// </summary>
        /// <param name="model">数据实体</param> 
        /// 创建人：jinchuanliang 时间：2016-09-22
        /// <returns></returns>
        public int Insert(FamilyInfoEntity model)
        {
            int id = SugarClient.Insertable(model).ExecuteReturnIdentity();
            return id;
        }


        /// <summary>
        /// 修改tb_familyInfo数据
        /// </summary>
        /// <param name="model">数据实体</param> 
        /// 创建人：jinchuanliang 时间：2018-07-14
        /// <returns></returns>
        public bool Update(FamilyInfoEntity model)
        {
            int i = SugarClient.Updateable(model).ExecuteCommand();

            return i == 1;
        }

        /// <summary>
        /// 删除tb_familyInfo数据
        /// </summary>
        /// <param name="familyInfoId">家族ID</param> 
        /// 创建人：jinchuanliang 时间：2018-07-14
        /// <returns></returns>
        public bool SignDelete(int familyInfoId)
        {
            //return Dal.SignDelete(familyInfoId);
            return false;
        }


        /// <summary>
        ///  获取ModelList
        /// </summary> 
        /// <param name="familyInfoId"></param>
        /// <returns></returns>
        public FamilyInfoEntity GetModel(int familyInfoId)
        {
            return SugarClient.Queryable<FamilyInfoEntity>().Where(w => w.IsDelete == 0 && w.FamilyInfoId == familyInfoId).First();
        }

        /// <summary>
        ///  获取ModelList
        /// </summary>  
        /// <returns></returns>
        public List<FamilyInfoEntity> GetModels()
        {
            return SugarClient.Queryable<FamilyInfoEntity>().Where(w => w.IsDelete == 0).ToList();
        }

        
        List<FamilyInfoEntity> Members;
        public List<FamilyInfoEntity> GetFamilyInfos(int fid)
        { 
            Members = SugarClient.Queryable<FamilyInfoEntity>().Where(w => w.IsDelete == 0 && w.FamilyInfoId == fid).ToList();

            List<FamilyInfoEntity> Menus = new List<FamilyInfoEntity>();
            FillMenus2(Menus, 0);

            return Menus;
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentId"></param>
        private void FillMenus2(List<FamilyInfoEntity> menus, int parentId)
        {
            if (Members == null) return;
            try
            {
                var sub = Members.Where(w => w.ParentId == parentId).ToList();

                if (sub.Count() > 0)
                {
                    foreach (var item in sub)
                    {
                        menus.Add(item);

                        FillMenus2(item.children = new List<FamilyInfoEntity>(), item.FamilyInfoId);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }



    }
}

